"""
给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。

岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外，你可以假设该网格的四条边均被水包围。

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""

def dfs(grid, r, c):
    grid[r][c] = 0
    # for i in grid:
    #     print(i)
    # print('---------------------------------------------------------')
    nr, nc = len(grid), len(grid[0])
    # 上下左右
    for x, y in [(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)]:
        if 0 <= x < nr and 0 <= y < nc and grid[x][y] == "1":
            dfs(grid, x, y)
class Solution:
    def dfs(self, grid, r, c):
        grid[r][c] = 0
        # for i in grid:
        #     print(i)
        # print('---------------------------------------------------------')
        nr, nc = len(grid), len(grid[0])
        # 上下左右
        for x, y in [(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)]:
            if 0 <= x < nr and 0 <= y < nc and grid[x][y] == "1":
                self.dfs(self, grid, x, y)

    def numIslands(self, grid: [[str]]) -> int:
        nr = len(grid)
        if nr == 0:
            return 0
        nc = len(grid[0])

        num_islands = 0
        for r in range(nr):
            for c in range(nc):
                if grid[r][c] == "1":
                    num_islands += 1
                    dfs(grid, r, c)

        return num_islands

grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]


print(Solution.numIslands(Solution, grid))